package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.company.entity.*;
import com.fz.us.admin.company.service.*;
import com.fz.us.oa.core.entity.com.AllChange;
import com.fz.us.oa.core.entity.com.PersonnelInfo;
import com.fz.us.oa.core.entity.com.PersonnelUse;
import com.fz.us.oa.core.entity.com.Transfer;
import com.fz.us.oa.core.service.com.AllChangeService;
import com.fz.us.oa.core.service.com.PersonnelInfoService;
import com.fz.us.oa.core.service.com.PersonnelUseService;
import com.fz.us.oa.core.service.com.TransferService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZhuChunXiao on 2017/7/18.
 */
@Controller
@RequestMapping(value = "/com/transfer")
public class TransferController extends BaseController {
    private static final long serialVersionUID = -2239996082204740539L;

    @Autowired
    private TransferService transferService;
    @Autowired
    private PersonnelInfoService personnelInfoService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private PostService postService;
    @Autowired
    private AllChangeService allChangeService;
    @Autowired
    private PersonnelUseService personnelUseService;
    @Autowired
    private DutyService dutyService;
    @Autowired
    private TaskEntityService taskEntityService;
    @Autowired
    private RemindService remindService;

    /**
     * 新建，编辑页面
     */
    @RequestMapping(value = "input", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String input(@CurrentUser User user, Model model,String keyId,String parentId,String draft,String index,String todo,String remind,String record) {
        if (StringUtils.isNotEmpty(keyId)){
            Transfer transfer=transferService.get(keyId);
            model.addAttribute("keyId", keyId);
            model.addAttribute("transfer", transfer);
        }
        PersonnelInfo personnelInfo=personnelInfoService.get(parentId);
        model.addAttribute("parentId", parentId);
        model.addAttribute("personnelInfo", personnelInfo);
        model.addAttribute("draft", draft);
        model.addAttribute("index", index);
        model.addAttribute("todo", todo);
        model.addAttribute("remind", remind);
        model.addAttribute("record", record);
        return "/com/transfer_input";
    }

    /**
     * 只读页面
     */
    @RequestMapping(value = "read", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String read(@CurrentUser User user, Model model,String keyId,String draft,String index,String todo,String remind,String record) {
        if(StringUtils.isNotEmpty(keyId)) {
            Transfer transfer=transferService.get(keyId);
            TaskEntity taskEntity = taskEntityService.getInfoByKeyId(keyId);
            int stepNo = taskEntity.getStepNo();
            String isEdit =  isEdit(keyId);
            model.addAttribute("isEdit", isEdit);
            model.addAttribute("transfer", transfer);
            model.addAttribute("stepNo", stepNo);
        }
        model.addAttribute("keyId", keyId);
        model.addAttribute("draft", draft);
        model.addAttribute("index", index);
        model.addAttribute("todo", todo);
        model.addAttribute("remind", remind);
        model.addAttribute("record", record);
        return "/com/transfer_read";
    }

    /**
     * 从异动总汇进入只读页面
     */
    @RequestMapping(value = "toRead",method = RequestMethod.GET,produces = MediaType.TEXT_HTML_VALUE)
    public String toRead(@CurrentUser User user, Model model,String allChangeId,String keyId,String draft,String index,String todo,String remind,String record){
        AllChange allChange=allChangeService.get(allChangeId);
        Transfer transfer=allChange.getTransfer();
        TaskEntity taskEntity = taskEntityService.getInfoByKeyId(transfer.getId());
        int stepNo = taskEntity.getStepNo();
        String isEdit =  isEdit(transfer.getId());
        model.addAttribute("isEdit", isEdit);
        model.addAttribute("transfer", transfer);
        model.addAttribute("stepNo", stepNo);
        model.addAttribute("keyId", keyId);
        model.addAttribute("draft", draft);
        model.addAttribute("index", index);
        model.addAttribute("todo", todo);
        model.addAttribute("remind", remind);
        model.addAttribute("record", record);

        return "/com/transfer_read";
    }

    /**
     * 保存Model数据
     */
    public Transfer saveModel(User user, String keyId, String parentId, String reason, String deptId, String postId, Double oldSalary, Double newSalary){
        Transfer transfer=null;
        if(org.apache.commons.lang.StringUtils.isBlank(keyId)){
            transfer=new Transfer();
            transfer.setCreater(user);
            transfer.setCompany(user.getCompany());
            transfer.setName("调动申请");
        }else{
            transfer=transferService.get(keyId);
        }
        if(org.apache.commons.lang.StringUtils.isNotEmpty(parentId)){
            transfer.setPersonnelInfo(personnelInfoService.get(parentId));
        }
        if(org.apache.commons.lang.StringUtils.isNotEmpty(reason)){
            transfer.setReason(reason);
        }
        if(org.apache.commons.lang.StringUtils.isNotEmpty(deptId)){
            transfer.setDepartment(departmentService.get(deptId));
        }
        if(org.apache.commons.lang.StringUtils.isNotEmpty(postId)){
            transfer.setPost(postService.get(postId));
        }
        if(oldSalary!=0){
            transfer.setOldSalary(oldSalary);
        }
        if(newSalary!=0){
            transfer.setNewSalary(newSalary);
        }

        return transfer;
    }

    /**
     * 保存操作
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public Object save(@CurrentUser User user,
                       @RequestParam(required = false) String keyId,
                       String parentId,
                       String reason,
                       String deptId,
                       String postId,
                       Double oldSalary,
                       Double newSalary) throws ParseException {

        Map<String,Object> rMap=new HashMap<String,Object>();
        Transfer transfer=saveModel(user, keyId, parentId, reason, deptId, postId, oldSalary, newSalary);

        if(StringUtils.isNotEmpty(keyId)){
            transferService.update(transfer);
            rMap.put("message", "更新成功");
        } else {
            transferService.save(transfer, "com.fz.us.oa.core.entity.com.Transfer");
            rMap.put("message", "保存成功");
        }
        rMap.put("state", "1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

    /**
     * 提交操作
     */
    @RequestMapping(value = "submit", method = RequestMethod.POST)
    public Object submit(@CurrentUser User user,
                         @RequestParam(required = false) String keyId,
                         String parentId,
                         String reason,
                         String deptId,
                         String postId,
                         Double oldSalary,
                         Double newSalary,
                         String curDutyId,
                         String comment) throws ParseException {

        Map<String, Object> rMap = new HashMap<String, Object>();
        Transfer transfer=saveModel(user,keyId,parentId,reason,deptId,postId,oldSalary,newSalary);

        if (StringUtils.isNotEmpty(keyId)) {
            transferService.approve(transfer, dutyService.get(curDutyId), comment);
        } else {
            transferService.commit(transfer, "com.fz.us.oa.core.entity.com.Transfer", dutyService.get(curDutyId));
        }

        //新建异动总汇表
        AllChange allChange=new AllChange();
        allChange.setType("4");
        allChange.setTransfer(transfer);
        allChange.setPersonnelInfo(personnelInfoService.get(parentId));
        allChange.setName("调动申请");
        allChange.setCreater(user);
        allChange.setCompany(user.getCompany());
        allChange.setIsFinished("0");
        allChangeService.commit(allChange, "com.fz.us.oa.core.entity.com.AllChange", dutyService.get(curDutyId));

        transfer.setAllChange(allChange);
        transferService.update(transfer);

        if(transfer.getProcessState().name().equals("Finished")){
            doFinished(transfer,parentId,deptId,postId,newSalary);
        }

        rMap.put("message", "提交成功");
        rMap.put("state", "1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

    //审批
    @RequestMapping(value = "approve", method = RequestMethod.POST)
    public Object approve(@CurrentUser User user, String keyId,  String curDutyId, String comment, String parentId, String deptId, String postId, Double newSalary){
        Map<String,Object> rMap=new HashMap<String,Object>();
        Transfer transfer = transferService.get(keyId);
        transferService.approve(transfer, dutyService.get(curDutyId), comment);

        //归档后...
        if(transfer.getProcessState().name().equals("Finished")){
            doFinished(transfer,parentId,deptId,postId,newSalary);
        }
        rMap.put("message","提交成功");
        rMap.put("state","1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

    public void doFinished(Transfer transfer,String parentId,String deptId,String postId,Double newSalary){
        //完成总汇表
        AllChange allChange=transfer.getAllChange();
        allChange.setIsFinished("1");
        allChangeService.update(allChange);

        //修改系统中的部门岗位
        PersonnelInfo personnelInfo=personnelInfoService.get(parentId);
        User changeUser=personnelInfo.getUser();
        Power oldPower=personnelInfoService.getPowerByDepartmentAndPost(personnelInfo.getDepartment(), personnelInfo.getPost());
        String oldDutyId=personnelInfoService.getDutyByPowerAndUser(oldPower,changeUser).getId();
        System.out.println("####" + oldDutyId);
        Power newPower=personnelInfoService.getPowerByDepartmentAndPost(departmentService.get(deptId),postService.get(postId));
        Duty newDuty=new Duty();
        newDuty.setPower(newPower);
        newDuty.setUser(changeUser);
        dutyService.updateEntity(changeUser, oldDutyId, newDuty);
        System.out.println("#####修改了职责");

        //修改用工信息 薪资
        PersonnelUse personnelUse=personnelInfo.getPersonnelUse();
        personnelUse.setSalary(newSalary);
        personnelUseService.update(personnelUse);

        //修改人事信息表中的部门岗位
        personnelInfo.setDepartment(departmentService.get(deptId));
        personnelInfo.setPost(postService.get(postId));
        personnelInfoService.update(personnelInfo);

        //发送提醒
        Remind remind = new Remind();
        remind.setUser(personnelInfoService.get(parentId).getUser());
        remind.setBussinessKey("transfer");
        remind.setKeyId(transfer.getId());
        remind.setContent("您的调动申请已通过，请查看");
        remindService.save(remind);
    }

    /**
     * 退回
     * @return
     */
    @RequestMapping(value = "back", method = RequestMethod.POST)
    public Object back(@CurrentUser User user, String keyId,String curDutyId, String comment) {
        if (StringUtils.isNotEmpty(keyId)) {
            Transfer transfer = transferService.get(keyId);
            transferService.back(transfer, dutyService.get(curDutyId), comment);
        }
        Map<String,Object> rMap=new HashMap<String,Object>();
        rMap.put("message","退回成功");
        rMap.put("state","1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }
    /**
     * 否决
     * @return
     */
    @RequestMapping(value = "deny", method = RequestMethod.POST)
    public Object deny(@CurrentUser User user, String keyId,String curDutyId, String comment) {
        if (StringUtils.isNotEmpty(keyId)) {
            Transfer transfer = transferService.get(keyId);
            transferService.deny(transfer, dutyService.get(curDutyId), comment);
        }
        Map<String,Object> rMap=new HashMap<String,Object>();
        rMap.put("message","否决成功");
        rMap.put("state","1");
        rMap.put("keyId", keyId);
        return ajaxJson(rMap);
    }

}
